package org.september.pisces.settings.service;

import org.september.core.constant.enums.DeleteFlag;
import org.september.pisces.settings.api.IPiscecSystemSettting;
import org.september.pisces.settings.entity.SystemSettings;
import org.september.smartdao.common.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PiscesSettingService extends BaseService {
	
	@Autowired
	private InnerSettingService innerSettingService;
	
	public String getValueOrDefault(String group , String key,String value, String label,String remark) {
		SystemSettings po = innerSettingService.getByKey(group ,key);
		if(po!=null) {
			return po.getCfgVal();
		}else {
			SystemSettings setting = new SystemSettings();
			setting.setCfgKey(key);
			setting.setDeleteFlag(DeleteFlag.NotDelete.ordinal());
			
			setting.setCfgVal(value);
			setting.setRemark(remark);
			setting.setLabel(label);
			setting.setGroupName(group);
			save(setting);
			return setting.getCfgVal();
		}
	}
	
	public void update(String group , String key,String value, String label,String remark) {
		SystemSettings setting = new SystemSettings();
		setting.setCfgKey(key);
		setting.setGroupName(group);
		setting.setDeleteFlag(DeleteFlag.NotDelete.ordinal());
		SystemSettings po = getCommonDao().getByExample(setting);
		
		if(po==null) {
			return;
		}else {
			po.setCfgVal(value);
			po.setLabel(label);
			po.setRemark(remark);
			update(po);
		}
	}
	
	public String getValueOrDefault(IPiscecSystemSettting settingVo) {
		return getValueOrDefault(settingVo.getGroup(),settingVo.getKey(),settingVo.getValue(),settingVo.getLabel(),settingVo.getRemark());
	}
}
